package com.wf.course.web.controller.ad;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.utils.page.PageBean;
import com.wf.course.model.ad.IndexRecommend;
import com.wf.course.model.ad.IndexType;
import com.wf.course.model.ad.VipCard;
import com.wf.course.model.course.CourseChapter;
import com.wf.course.model.course.CourseInfo;
import com.wf.course.service.ICourseChapterService;
import com.wf.course.service.ICourseInfoService;
import com.wf.course.service.IIndexRecommendService;
import com.wf.course.service.IIndexTypeService;
import com.wf.course.service.IVipCardService;
import com.wf.course.web.Interceptor.LogInfo;
/**
 * 
 * @content 课程分类Controller
 */
@RequestMapping("indexRecommend")
@Controller
public class IndexRecommendController {

	@Autowired
	private IIndexRecommendService indexRecommendService;
	
	@Autowired
	private ICourseInfoService courseInfoService;
	
	@Autowired
	private ICourseChapterService chapterService;
	@Autowired
	private IIndexTypeService indexTypeService;
	@Autowired
	private IVipCardService cardService;

	/**
	 * 列表展示
	 * @param pageNo
	 * @param pageSize
	 * @param indexTypeCode
	 * @param buyType
	 * @param displayType
	 * @param model
	 * @return
	 */
	@LogInfo("查询首页分类对应课程列表")
	@RequestMapping("indexRecommendList")
	public String adList(@RequestParam(value = "pageNo", required = false) Integer pageNo,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			@RequestParam(value = "indexTypeCode", required = false) String indexTypeCode, 
			@RequestParam(value = "buyType", required = false) String buyType, 
			@RequestParam(value = "displayType", required = false) String displayType, 
			Model model) {

		// 组装一个查询条件的map
		Map<String, Object> cons = new HashMap<String, Object>();
		cons.put("indexTypeCode", indexTypeCode);
		cons.put("buyType", buyType);
		cons.put("displayType", displayType);
		PageBean<IndexRecommend> pageBean = new PageBean<IndexRecommend>(pageNo, pageSize, null, cons);
		pageBean.setUrl("indexRecommend/indexRecommendList");
		this.indexRecommendService.indexInfo(pageBean);
		
		 List<IndexType> types = indexTypeService.queryTypes();
		 model.addAttribute("types",types);
		
		model.addAttribute("page", pageBean);
		
		return "ad/indexRecommendList";
	}

	/**
	 * 修改和新增页面数据初始化
	 * @param id
	 * @param model
	 * @return
	 */
	@LogInfo("添加或修改首页分类课程，页面初始化")
	@RequestMapping("pageLoad/{id}")
	public String downLoadById(@PathVariable("id") Integer id, Model model) {
		Integer courseId = 0; 
		if (id != null && id != 0) {
			IndexRecommend recommend = indexRecommendService.queryIndexRecommendById(id);
			model.addAttribute("recommend", recommend);
			model.addAttribute("type", "upd");
			//获取该分类信息章节id，如果不是按照章节id，则为-1
			Integer courserChapterId = recommend.getChapterId();
			if (courserChapterId !=null && courserChapterId>0) {
				CourseChapter cpr = chapterService.queryCourseChapterById(courserChapterId);
				courseId = cpr.getCourseId();
			}
		}else{
			model.addAttribute("type", "add");
		}
		 List<CourseInfo> courses = courseInfoService.queryCourses();
		 model.addAttribute("courses",courses);
		 
		 List<CourseChapter> chapters = null;
		 if (courseId > 0) {
			chapters = chapterService.courseChapterByCourseId(courseId);
		}else{
			 chapters = chapterService.queryCourseChapters();
		} 
		 model.addAttribute("chapters",chapters);
		 model.addAttribute("courseId",courseId);
		 
		 List<VipCard> cards = cardService.queryVipCards();
		 model.addAttribute("cards",cards);
		 
		 List<IndexType> types = indexTypeService.queryTypes();
		 model.addAttribute("types",types);
		
		return "ad/indexRecommendEdit";
	}

	/**
	 * 新增和修改
	 * @param recommend
	 * @param request
	 * @return
	 */
	@LogInfo("添加或修改首页分类课程，页面初始化")
	@RequestMapping(value = "indexRecommendEdit" , produces = "text/html;charset=UTF-8")
	public String indexTypeEdit(@ModelAttribute IndexRecommend recommend,  HttpServletRequest request) {
		
		indexRecommendService.saveOrUpdateIndexRecommend(recommend);
		
		return "redirect:/indexRecommend/indexRecommendList";
	}

	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@LogInfo("删除首页分类课程")
	@RequestMapping(value = "remove/{id}")
	public String removeAd(@PathVariable("id") Integer id) {
		
		indexRecommendService.deleteById(id);
		
		return "redirect:/indexRecommend/indexRecommendList";
	}
	
	/**
	 * 上移或下移排序 0上移  1下移
	 * @param indexTypeCode
	 * @param orderNum
	 * @param orderType
	 * @return
	 */
	@ResponseBody
	@LogInfo("上移或下移排序")
	@RequestMapping(value="sorting",method=RequestMethod.GET,produces="text/html;charset=UTF-8")
	public String sorting(
				@RequestParam("indexTypeCode") Integer indexTypeCode,
				@RequestParam("orderNum") Integer orderNum,
				@RequestParam("orderType") Integer orderType,
				@RequestParam("id") Integer id
			){
		boolean b = indexRecommendService.sorting(indexTypeCode, orderNum, orderType,id);
		return b?"1":"0";
	}
	
	@ResponseBody
	@LogInfo("根据课程ID，获取该课程下所有的章节")
	@RequestMapping(value="queryCourseChapter",method=RequestMethod.GET,produces="text/html;charset=UTF-8" )
	public String queryCourseChaptersByCourseId(@RequestParam("courseId") Integer courseId){
		return chapterService.queryCourseChaptersByCourseId(courseId);
	}
}
